<?php

namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
 * App\Models\User
 *
 * @property int $id
 * @property int|null $uniacid
 * @property int|null $weuid
 * @property string|null $unionid
 * @property string|null $password
 * @property string|null $openid
 * @property string|null $wxapp_openid
 * @property string|null $nickname
 * @property int|null $level_id 等级id
 * @property string|null $avatar
 * @property string|null $realname
 * @property string|null $mobile
 * @property string|null $gender
 * @property string|null $province
 * @property string|null $city
 * @property string|null $delivery_address 送货地址
 * @property string|null $ip
 * @property int|null $state
 * @property int|null $usertype
 * @property string|null $country
 * @property string|null $balance
 * @property string|null $vircoin 瓜豆
 * @property int|null $free_gift 免费礼物
 * @property string|null $gb_num 瓜币余额
 * @property int|null $pay_count
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property int|null $deleted_at
 * @property int|null $parent_uid
 * @property int|null $child_nums
 * @property string|null $idcard
 * @property string|null $pay_money
 * @property string|null $recharge_money
 * @property string|null $give_balance
 * @property int|null $credit
 * @property int|null $follow_num
 * @property int|null $follower_num
 * @property int|null $is_superman 是不是超管
 * @property int|null $is_teamleader 是否团长
 * @property int|null $supplier_id 供货商id
 * @property int|null $last_login_time 最后一次登录的时间
 * @property int|null $is_socileader 是不是社区团长
 * @property int|null $cai_id
 * @property int|null $school_id
 * @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|User query()
 * @method static \Illuminate\Database\Eloquent\Builder|User whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereBalance($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereCaiId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereChildNums($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereCountry($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereCredit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereDeliveryAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereFollowNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereFollowerNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereFreeGift($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereGbNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereGender($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereGiveBalance($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereIdcard($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereIsSocileader($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereIsSuperman($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereIsTeamleader($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereLastLoginTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereLevelId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereNickname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereOpenid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereParentUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User wherePayCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User wherePayMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereRealname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereRechargeMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereSchoolId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereSupplierId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereUniacid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereUnionid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereUsertype($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereVircoin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereWeuid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereWxappOpenid($value)
 * @mixin \Eloquent
 * @property string|null $remark 备注
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserAddress[] $addresses
 * @property-read int|null $addresses_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Shop[] $shops
 * @property-read int|null $shops_count
 * @method static \Illuminate\Database\Query\Builder|User onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|User whereRemark($value)
 * @method static \Illuminate\Database\Query\Builder|User withTrashed()
 * @method static \Illuminate\Database\Query\Builder|User withoutTrashed()
 * @property int|null $is_leader
 * @method static \Illuminate\Database\Eloquent\Builder|User whereIsLeader($value)
 * @property-read \App\Models\Escort|null $escort
 */
class User extends Authenticatable implements JWTSubject
{
    use SoftDeletes;
    protected $primaryKey = 'id';
    protected $dateFormat = 'U';
    protected $guarded = [];
    protected static $users;
    protected $hidden = ['openid', 'wxapp_openid', 'updated_at', 'created_at', 'deleted_at', 'unionid'];
    protected $appends = ['shops'];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function shops()
    {
        return $this->belongsToMany(Shop::class, 'user_shops', 'uid', 'shop_id')
            ->wherePivot('deleted_at', null);
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    public function getAvatarAttribute($value)
    {
        if(!$value){
            return asset('/static/img/avatar.png');
        }
    }

    public function getShopsAttribute()
    {
        return $this->shops()->select('name', 'address', 'avatar', 'phone', 'realname')->get();
    }

    public function addresses()
    {
        return $this->hasMany(UserAddress::class, 'uid', 'id')->select('id', 'uid', 'province', 'city', 'district', 'address_info');
    }

    public function escort()
    {
        return $this->hasOne(Escort::class, 'uid', 'id');
    }

    /**
     * 通过id获取用户信息
     * @param $uid
     * @return self
     */
    public static function getByUid($uid)
    {
        $result = null;
        if (!$uid) {
            return;
        }

        if (isset(self::$users[$uid])) {
            $result = self::$users[$uid];
        } else {
            $result = self::find($uid);
            self::$users[$uid] = $result;
        }

        return $result;
    }
}